package com.yylending.las.lending.quota.idcard;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yylending.las.R;
import com.yylending.las.YJKD;
import com.yylending.las.authority.login.LoginActivity;
import com.yylending.las.base.BaseFragment;
import com.yylending.las.common.utils.FormatUtils;
import com.yylending.las.lending.quota.ApplyQuotaActivity;

/**
 * 身份证信息确认界面
 * Created by laixj on 2016/11/17.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class IdCardInfoConfirmFragment extends BaseFragment {
	private final static String TAG = "IdCardInfoConfirmFragment";

	private final static String ARG_ID_CARD = "id_card";

	@BindView(R.id.confirm_idcard_info_name)
	TextView mNameTv;
	@BindView(R.id.confirm_idcard_info_number)
	TextView mNumberTv;
	@BindView(R.id.confirm_idcard_info_office)
	TextView mOfficeTv;
	@BindView(R.id.confirm_idcard_info_period)
	TextView mPeriodTv;
	@BindView(R.id.confirm_idcard_info_next)
	Button mIdcardInfoNext;
	Unbinder unbinder;

	private IdCardModel.IdCard mIdCard;

	public static BaseFragment newInstance(int sectionNumber, Object param) {
		BaseFragment fragment = new IdCardInfoConfirmFragment();
		Bundle args = new Bundle();
		args.putInt(ARG_SECTION_NUMBER, sectionNumber);
		if (null != param)
			args.putParcelable(ARG_ID_CARD, (IdCardModel.IdCard) param);
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle bundle = getArguments();
		if (null != bundle) {
			mIdCard = bundle.getParcelable(ARG_ID_CARD);
		}
		if (!YJKD.login()) {
			LoginActivity.start((RxAppCompatActivity) getActivity());
		}
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		switch (id) {
			default:
				return null;
		}
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		unbinder.unbind();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_edit_idcard_information_confrim, null);
		unbinder = ButterKnife.bind(this, v);
		initData();
		return v;
	}

	private void initData() {
		if (null != mIdCard && !TextUtils.isEmpty(mIdCard.inumber)) {
			mNameTv.setText(mIdCard.iname);
			mNumberTv.setText(FormatUtils.displayIDNumber(mIdCard.inumber));
			mOfficeTv.setText(mIdCard.ioffice);
			mPeriodTv.setText(FormatUtils.formatIdCardValidDate(mIdCard.istart) + "-" + FormatUtils.formatIdCardValidDate(mIdCard.iend));
		} else {
			mNameTv.setText("");
			mNumberTv.setText("");
			mOfficeTv.setText("");
			mPeriodTv.setText("");
		}
	}

	@OnClick(R.id.confirm_idcard_info_next)
	public void onViewClicked(View v) {
		switch (v.getId()) {
			case R.id.confirm_idcard_info_next:
				if (YJKD.isRealName()) {
					((ApplyQuotaActivity) getActivity()).showFragment(ApplyQuotaActivity.PAGE_FACE, null);
				}
				break;
		}
	}
}
